package com.imooc.imoocbilibili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.imoocbilibili.entity.*;
import com.imooc.imoocbilibili.interceptor.ThreadUser;
import com.imooc.imoocbilibili.service.*;
import com.imooc.imoocbilibili.vo.UserAuthVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.imooc.imoocbilibili.mapper.AuthRoleMapper;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service("authRoleService")
@RequiredArgsConstructor
public class AuthRoleServiceImpl extends ServiceImpl<AuthRoleMapper, AuthRole> implements AuthRoleService {

    private final UserRoleService userRoleService;
    private final AuthRoleElementOperationService authRoleElementOperationService;
    private final AuthElementOperationService authElementOperationService;
    private final AuthRoleMenuService authRoleMenuService;
    private final AuthMenuService authMenuService;

    @Override
    public Page<AuthRole> queryPage(Integer pageNo, Integer pageSize) {
        Page<AuthRole> page = this.page(
                new Page<>(pageNo, pageSize),
                // 拼接查询条件
                new LambdaQueryWrapper<AuthRole>()
        );

        return page;
    }

    @Override
    public UserAuthVO getUserAuthorities() {
        User user = ThreadUser.getUser();
        Long userId = user.getId();
        List<UserRole> userRoleList = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserid, userId));

        Set<Long> roleIdSet = userRoleList.stream().map(UserRole::getRoleid).collect(Collectors.toSet());
        List<AuthRoleElementOperation> authRoleElementOperations = authRoleElementOperationService.list(new LambdaQueryWrapper<AuthRoleElementOperation>()
                .in(AuthRoleElementOperation::getRoleid, roleIdSet));
        for (AuthRoleElementOperation authRoleElementOperation : authRoleElementOperations) {
            Long elementoperationid = authRoleElementOperation.getElementoperationid();
            AuthElementOperation authElementOperation = authElementOperationService.getOne(new LambdaQueryWrapper<AuthElementOperation>()
                    .eq(AuthElementOperation::getId, elementoperationid));
            authRoleElementOperation.setAuthElementOperation(authElementOperation);
        }

        List<AuthRoleMenu> authRoleMenus = authRoleMenuService.list(new LambdaQueryWrapper<AuthRoleMenu>()
                .in(AuthRoleMenu::getRoleid, roleIdSet));
        for (AuthRoleMenu authRoleMenu : authRoleMenus) {
            Long menuid = authRoleMenu.getMenuid();
            AuthMenu authMenu = authMenuService.getOne(new LambdaQueryWrapper<AuthMenu>()
                    .eq(AuthMenu::getId, menuid));
            authRoleMenu.setAuthMenu(authMenu);
        }

        UserAuthVO userAuthVO = new UserAuthVO();
        userAuthVO.setRoleElementOperationList(authRoleElementOperations);
        userAuthVO.setRoleMenuList(authRoleMenus);
        return userAuthVO;
    }

}